<template>
	<view class="Scan_payment">
		<view class="title">
			<image src="../../static/image/back.png" class="back" @tap="back"></image>
			<text>{{i18n.smzf}}</text>
		</view>
		<image src="../../static/image/avator.png" class="avator"></image>
		<view class="uName">{{account_info.accountName}}</view>
		
		<view class="pay_block">
			<view class="assetName">
				<text>{{i18n.Token}}:</text>
				<input type="text" :value="result.Tocoin_name" />
			</view>
			<view class="amoutn_box">
				<text>{{i18n.shuliang}}:</text>
				<text class="amount_text" style="width: 85%;height: auto;word-break:break-all;" v-if="result.Toamount">{{amount}}</text>
				<input type="number" v-else  placeholder="请输入支付数量" v-model="amount" 
				style="border: none;padding-left: 20upx;width: 80%;"
				placeholder-style="font-size:13px;color:#8687A3" />
			</view>
			<view class="accoutn">
				<text>{{i18n.receiptaccount}}:</text>
				<text class="Toaccount">{{result.ToaccountName}}</text>
			</view>
		</view>
		
		<image :src="i18n.qdxzimg" class="payment_btn" @tap="payment"></image>
		<password v-if="show" :gasPrice="100" :gasLimit="1000000" @close="close" @callback="callback" :yzpassword="i18n.yzpassword" :btnl="i18n.btn1" :btnr="i18n.btn2" :placeholder="i18n.placeholder" :passprice="i18n.passprice" :pwdtitle="i18n.pwdtitle" :cap="i18n.cap"></password>
	</view>
</template>

<script>
	import * as oex from 'oex-web3'; 
	import {myMixins} from '../../common/mixins.js'
	import password from '../../components/password/password.vue'
	// import BigNumber from 'bignumber.js';
	import payment from '../../common/payment.js'
	export default {
		computed: {
			i18n() {
				return this.$t('user')
			}
		},
		mixins:[myMixins],
		data() {
			return {
				result:"",//二维码内容
				amount:"",
				password:"",
				show: false, //支付密码
				assetId:"",
				decimals:"",
				symbol:"",
				currentBalances:"",
				accountName:""
			}
		},
		onLoad(options) {
			this.accountName = uni.getStorageSync('account_info').accountName;
			this.result = JSON.parse(options.result);
			this.amount = JSON.parse(options.result).Toamount;
			//资产ID
			 this.getAssetId(this.result.assetId);
				this.assetId = this.result.assetId
				this.decimals=this.result.decimals
				this.symbol=this.result.symbol
		},
		components:{password},
		methods: {
			async getAssetId(assetId){
				const _this = this;
				
				oex.utils.setApp();
				const nodeInfo = uni.getStorageSync('nodeInfo');
				//查余额
				var balances = await oex.account.getAccountByName(this.accountName);
				this.$CommonJS.request(nodeInfo,"POST",balances).then( res => {
						const b = res.data.result.balances;
						for(let i = 0; i < b.length; i++){
							// if(_this.assetInfo){
								if(b[i].assetID == assetId){
									_this.currentBalances = b[i].balance/Math.pow(10,_this.decimals)
									// console.log(_this.currentBalances)
									break;
								}
							// }
						}
				}).catch(error => {
						console.log(error)
				})
				
			},
			back(){
				this.$CommonJS.navigateBack()
			},
			payment(){
				const _this=this
				if(!this.amount){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '请填写支付金额':'Please fill in the payment amount')
					return;
				}
				console.log(this.currentBalances);
				if(Number(this.amount) > Number(this.currentBalances) ){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '账户余额不足':'Insufficient account balance');
					return;
				}
				this.show = true;
				
			},
			close(){
				//关闭支付框
				this.show = false
			},
			callback(privateKey,gasPrice,gasLimit){
				const _this = this;
				//console.log(privateKey,gasPrice,gasLimit);
				this.show = false
				this.account_info.accountName = this.account_info.accountName.trim();
				this.result.ToaccountName = this.result.ToaccountName.trim();
				this.assetId = Number(this.assetId);
				//支付参数(currentAccountName,toAccountName,assetId,amount,decimals,privateKey,gasPrice,gasLimit)
				//console.log(this.amount);
				payment.payment(this.account_info.accountName,this.result.ToaccountName,this.assetId,Number(this.amount),'', this.decimals,privateKey,gasPrice,gasLimit).then(paymentInfo =>{
					if(paymentInfo){
						_this.$CommonJS.navigateTo('./payment_success?symbol=' + _this.symbol + '&amount=' + _this.amount + '&ToaccountName=' + _this.result.ToaccountName)
					}
				}).catch(err =>{
					console.log(err)
				})
			},
		}
	}
</script>

<style lang="stylus">
	@import "../../static/stylus/scan_payment/payment.stylus"
	.amoutn_box
		width 100% 
		display flex
		flex-direction row
		justify-content flex-start
		align-items center
		font-size 14px 
		color #FFFFFF
		padding  10upx 0
		
	.amount_text 
		font-size 18px 
		font-weight bold 
		margin-left 20upx
</style>
